//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东（ShirDon）
// Blog:https://www.shirdon.com/
// 仓库地址：https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址：https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++

package main

import (
	"fmt"

	"github.com/gomodule/redigo/redis"
)

func main() {
	setdb := redis.DialDatabase(1)
	// setPw := redis.DialPassword("CC72E8782F689677B862CF13D410B121F1D72EE39313AC807AC5FABBA9287")
	// c, err := redis.Dial("tcp", "127.0.0.1:6379", setdb, setPw)
	setPw := redis.DialPassword("BA72E8782F689677B862CF13D410B121F1D72EE39313AC807AC5FABBA9287")
	c, err := redis.Dial("tcp", "192.168.9.150:16379", setdb, setPw)
	if err != nil {
		fmt.Println("conn redis failed, err:", err)
		return
	}
	defer c.Close()

	fmt.Println("#################")
	_, err = c.Do("Set", "username", "jim")
	if err != nil {
		fmt.Println(err)
		return
	}
	//
	fmt.Println("#################")
	res, err := redis.String(c.Do("Get", "username"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(res)
	//
	fmt.Println("#################")
	_, err = c.Do("MSet", "username", "james", "phone", "18888888888")
	if err != nil {
		fmt.Println("MSet error: ", err)
		return
	}
	//
	fmt.Println("#################")
	res2, err := redis.Strings(c.Do("MGet", "username", "phone"))
	if err != nil {
		fmt.Println("MGet error: ", err)
		return
	}
	fmt.Println(res2)
	//
	//
	//
	fmt.Println("#################")
	_, err = c.Do("HSet", "names", "jim", "中文")
	if err != nil {
		fmt.Println("hset error: ", err)
		return
	}
	//
	fmt.Println("#################")
	res3, err := redis.String(c.Do("HGet", "names", "jim"))
	if err != nil {
		fmt.Println("hget error: ", err)
		return
	}
	fmt.Println(res3)
	//
	//
	fmt.Println("#################")
	//expire
	_, err = c.Do("expire", "names", 10)
	if err != nil {
		fmt.Println("expire error: ", err)
		return
	}
	fmt.Println("#################")
	//队列
	_, err = c.Do("lpush", "Queue", "jim_1", "barry_2", 9)
	if err != nil {
		fmt.Println("lpush error: ", err)
		return
	}

	fmt.Println("#################")
	res4, err := redis.Int(c.Do("llen", "Queue"))
	if err != nil {
		fmt.Println("llen error: ", err)
		return
	}
	fmt.Println(res4)
	fmt.Println("#################")
	for {
		r, err := redis.String(c.Do("lpop", "Queue"))
		if err != nil {
			fmt.Println("lpop error: ", err)
			break
		}
		fmt.Println(r)
	}

	fmt.Println("#################")
	res4, err = redis.Int(c.Do("llen", "Queue"))
	if err != nil {
		fmt.Println("llen error: ", err)
		return
	}
	fmt.Println(res4)

}
